#include <codecvt>
#include <cwchar>
#include <iostream>
using std::cout;
using std::endl;
using std::string;

// https://stackoverflow.com/questions/12170488/how-to-get-current-locale-of-my-environment
void print_system_locale()
{
    cout << "Before set according to the environment variables" << endl;
    cout << "  LC_ALL: " << setlocale(LC_ALL, NULL) << endl;
    cout << "  LC_CTYPE: " << setlocale(LC_CTYPE, NULL) << endl;
    setlocale(LC_ALL, "");  // Locale categories
    cout << "After set according to the environment variables" << endl;
    cout << "  LC_ALL: " << setlocale(LC_ALL, NULL) << endl;
    cout << "  LC_CTYPE: " << setlocale(LC_CTYPE, NULL) << endl;
}

int main(int argc, char *argv[])
{
    print_system_locale();

    // https://cplusplus.com/reference/clocale/setlocale/
    std::locale::global(std::locale(""));  // Environment's default locale
    system("chcp");
    // system("chcp 65001");
    std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
    string ss = "Hi 测试中文";
    std::wstring ws = converter.from_bytes(ss);
    // 在codepage是936时，能正常显示中文
    std::wcout << "wcout: " << ws << std::endl;
    // 如果codepage是936，则打印乱码，因为Windows环境编译时指定了字符串为utf8
    cout << "Hello 测试中文" << endl;
    return 0;
}
